package com.renrenyou.duanshipin.login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;

import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.about.WebActivity;
import com.renrenyou.duanshipin.base.BaseActivity;
import com.renrenyou.duanshipin.bean.RegisterData;
import com.renrenyou.duanshipin.databinding.ActivityRegisterBinding;
import com.renrenyou.duanshipin.net.Constance;
import com.renrenyou.duanshipin.net.MyOkhttp;
import com.renrenyou.duanshipin.net.MyOkhttpCallback;
import com.renrenyou.duanshipin.util.ProgressDialogUtil;
import com.renrenyou.duanshipin.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> implements View.OnClickListener {

    private Call mCall;
    private AlertDialog mDialog;

    @Override
    public ActivityRegisterBinding getBinding(LayoutInflater inflater) {
        return ActivityRegisterBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.tvRegisterBtn.setOnClickListener(this);
        SpannableString sb = new SpannableString(getString(R.string.login_desc));
        sb.setSpan(new ForegroundColorSpan(getColor(R.color.red)), 7, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(getColor(R.color.red)), sb.length() - 6, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        sb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //隐私
                Intent intent = new Intent(RegisterActivity.this, WebActivity.class);
                intent.putExtra("title", "隐私政策");
                intent.putExtra("url", Constance.YINSI);
                startActivity(intent);
            }
        }, 7, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //协议
                Intent intent = new Intent(RegisterActivity.this, WebActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("url", Constance.XIEYI);
                startActivity(intent);
            }
        }, sb.length() - 6, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mBinding.tvRegisterDesc.setText(sb);
        mBinding.tvRegisterDesc.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_register_btn) {
            Editable phoneEditable = mBinding.edtRegisterPhone.getText();
            Editable pwdEditable = mBinding.edtRegisterPwd.getText();

            if (phoneEditable != null && pwdEditable != null) {
                String phone = phoneEditable.toString().trim();
                String pwd = pwdEditable.toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast(this, "请输入手机号");
                    return;
                }
                if (phone.length() != 11) {
                    ToastUtil.showToast(this, "手机号码必须为11位");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast(this, "请输入密码");
                    return;
                }
                if (!mBinding.tvRegisterDesc.isChecked()) {
                    showPolicyDialog();
                    return;
                }
                startRegister(phone, pwd);
            }
        }
    }

    private void showPolicyDialog() {
        PolicyDialog dialog = new PolicyDialog(this);
        dialog.setOnPositiveListener(new PolicyDialog.PositiveCallback() {
            @Override
            public void onPositive() {
                mBinding.tvRegisterDesc.setChecked(true);
                dialog.dismiss();
            }

            @Override
            public void onNegative() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void startRegister(String phone, String pwd) {
        Map<String, Object> params = new HashMap<>();
        params.put("appCode", "com.android.sucai2025");
        params.put("versionNo", "100");
        params.put("channel", "android");
        params.put("userName", phone);
        params.put("password", pwd);
        mDialog = ProgressDialogUtil.showProgressDialog(this);
        mCall = MyOkhttp.getInstance().post(Constance.BASE_URL + Constance.REGISTER, params,
                new MyOkhttpCallback<RegisterData>() {
                    @Override
                    public void onFailed(int errorCode, int errorMsg) {
                        mDialog.dismiss();
                        ToastUtil.showToast(RegisterActivity.this, getString(errorMsg));
                    }

                    @Override
                    public void onSuccess(RegisterData data) {
                        mDialog.dismiss();
                        if (data == null) {
                            ToastUtil.showToast(RegisterActivity.this, "注册失败");
                            return;
                        }
                        if (data.isSuccess()) {
                            ToastUtil.showToast(RegisterActivity.this, "注册成功");
                            finish();
                        } else {
                            ToastUtil.showToast(RegisterActivity.this, data.getMessage());
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        if (mCall != null) {
            mCall.cancel();
        }
        super.onDestroy();
    }
}
